var gulp = require('gulp');
var coffee = require('gulp-coffee');
var plumber = require('gulp-plumber');
var beep = require('beepbeep');

var concat = require('gulp-concat');
var jade = require('gulp-jade');
var uglify = require('gulp-uglify');
var sourcemaps = require('gulp-sourcemaps');
var declare = require('gulp-declare');
var _ = require('lodash');
var minifyCss = require('gulp-clean-css');

var sass = require('gulp-sass');
var stylus = require('gulp-stylus');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer');

function errorHandler(err) {
    beep(2);
    console.error(err.toString());
}

function relativePaths(parentPath, paths) {
    return _.map(paths, function (p) {
        return parentPath + p
    });
}

function enhancedSass(srcPaths, concatFile, descPath) {
    gulp.src(srcPaths)
        .pipe(plumber({errorHandler: errorHandler}))
        .pipe(sass())
        .pipe(concat(concatFile))
        .pipe(minifyCss({compatibility: 'ie8'}))
        .pipe(postcss([autoprefixer({browsers: ['last 2 versions']})]))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(descPath));
}

function coffeeScript(srcPaths, concatFile, destPath) {
    var p = gulp.src(srcPaths)
        .pipe(plumber({errorHandler: errorHandler}))
        .pipe(sourcemaps.init())
        .pipe(coffee());
    // .pipe(uglify())
    p = concatFile && p.pipe(concat(concatFile)) || p;
    p.pipe(sourcemaps.write('.'))
        .pipe(gulp.dest(destPath));
}

gulp.task('server', [], function () {
    gulp.src('server/src/**/*.coffee')
        .pipe(plumber({errorHandler: errorHandler}))
        .pipe(coffee())
        .pipe(gulp.dest('dist/server'));
});

gulp.task('jade', [], function () {
    gulp.src('server/src/jade/**/*.jade')
        .pipe(plumber({errorHandler: errorHandler}))
        .pipe(gulp.dest('dist/server/jade'));
});

gulp.task('config', [], function () {
    gulp.src('server/config.js')
        .pipe(plumber({errorHandler: errorHandler}))
        .pipe(gulp.dest('dist/server'));
});

gulp.task('front_template', [], function () {
    function compileJade(src, namespace, outFile) {
        gulp.src(src)
            .pipe(plumber({errorHandler: errorHandler}))
            .pipe(jade({client: true}))
            .pipe(declare({namespace: namespace, noRedeclare: true}))
            .pipe(concat(outFile))
            .pipe(uglify())
            .pipe(gulp.dest('dist/front/script'));
    }

    compileJade(['ui-front/template/common/**.jade', 'ui-front/template/common/**.html'], 'WCT', 'common-template.js');
    compileJade(['ui-front/template/view/**.jade'], 'WT', 'template.js');
});

gulp.task('front_style', [], function () {
    gulp.src(['ui-front/style/**.styl'])
        .pipe(plumber({errorHandler: errorHandler}))
        .pipe(stylus())
        .pipe(minifyCss({compatibility: 'ie8'}))
        .pipe(postcss([autoprefixer({browsers: ['last 2 versions']})]))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('dist/front/style'));
    gulp.src(['ui-front/style/ie/**.styl'])
        .pipe(plumber({errorHandler: errorHandler}))
        .pipe(stylus())
        .pipe(minifyCss({compatibility: 'ie8'}))
        .pipe(postcss([autoprefixer({browsers: ['last 2 versions']})]))
        .pipe(sourcemaps.write('.'))
        .pipe(concat('app.ie.css'))
        .pipe(gulp.dest('dist/front/style'));
});

gulp.task('front_script', [], function () {
    var dest = "dist/front/script";
    var srcPaths = relativePaths("ui-front/script/", ["setup.coffee", "common/**.coffee"]);
    coffeeScript(srcPaths, 'common.js', dest);
    coffeeScript('ui-front/script/view/**', null, dest);
});

gulp.task('front_lib', [], function () {
    gulp.src("ui-front/lib/**").pipe(gulp.dest('dist/front/lib'));
});

gulp.task('front_img', [], function () {
    return gulp.src("ui-front/img/**").pipe(gulp.dest('dist/front/img'));
});

gulp.task('front_resource', [], function () {
    return gulp.src("ui-front/resource/**").pipe(gulp.dest('dist/front/resource'));
});

gulp.task('console_index', [], function () {
    gulp.src(['console/src/index.jade'])
        .pipe(plumber({errorHandler: errorHandler}))
        .pipe(jade())
        .pipe(gulp.dest('dist/console'));
});

gulp.task('console_template', [], function () {
    gulp.src(['console/src/**/**.jade', '!console/src/index.jade'])
        .pipe(jade({client: true}))
        .pipe(declare({namespace: 'ST', noRedeclare: true}))
        .pipe(concat('template.js'))
        .pipe(gulp.dest('dist/console'));
});

gulp.task('console_style', [], function () {
    enhancedSass("console/src/**/**.scss", 'app.css', 'dist/console');
});

gulp.task('console_script', [], function () {
    coffeeScript(['console/src/setup.coffee', 'console/src/api.coffee', 'console/src/**/**.coffee'],
        "app.js", "dist/console");
});

gulp.task('console_lib', [], function () {
    gulp.src("console/lib/**").pipe(gulp.dest('dist/console/lib'));
});

gulp.task('console_img', [], function () {
    return gulp.src("console/img/**").pipe(gulp.dest('dist/console/img'));
});

gulp.task('watch', ['default'], function () {
    gulp.watch('server/src/**/*.coffee', ['server']);
    gulp.watch('server/config.js', ['config']);
    gulp.watch('server/src/jade/**/*.jade', ['jade']);

    gulp.watch(['ui-front/template/**'], ['front_template']);
    gulp.watch('ui-front/style/**/**.styl', ['front_style']);
    gulp.watch('ui-front/script/**', ['front_script']);
    gulp.watch('ui-front/img/**', ['front_img']);
    gulp.watch('ui-front/lib/**', ['front_lib']);
    gulp.watch('ui-front/source/**', ['front_resource']);

    gulp.watch('console/src/index.jade', ['console_index']);
    gulp.watch('console/src/**/**.jade', ['console_template']);
    gulp.watch('console/src/**/**.html', ['console_template']);
    gulp.watch('console/src/**/**.scss', ['console_style']);
    gulp.watch('console/src/**/**.coffee', ['console_script']);
    gulp.watch('console/img/**', ['console_img']);
    gulp.watch('console/lib/**', ['console_lib']);
});

gulp.task('default', ['server', 'config', 'jade',
    'front_template', 'front_style', 'front_script', 'front_img', 'front_lib', 'front_resource',
    'console_index', 'console_template', 'console_style', 'console_script', 'console_img', 'console_lib']);